#pragma once

#define RECV_BUFFER_SIZE			1024
#define	PROTOCOL_MSG_HEADSIZE		4
#define MAX_PACKETSIZE				1024
#define	MAX_MESSAGESIZE				MAX_PACKETSIZE + 24
#define SOCKET_SENDBUFSIZE			64*1024
#define MAX_MSGPACKSIZE				2048
#define STAT_MAX_MAPGROUP			5
#define	SOCKETKERNEL_ONTIMER_MS		1000	
#define	TIMER_OFFSET_LIMIT			10
#define	SOCKET_PROCESS_INTERVAL		40
#define	THREADDELAY_MS				400
#define	WORLDKERNEL_ONTIMER_MS		1000
#define ID_NONE						0
#define _MSG_NONE					0



#define	ASSERT(x)					(void)( (x) || (assert(!"(#x)"), 0) )
#define	CHECKF(x)					do{ if(!(x)) { assert(!(#x)); return 0; } }while(0)

#define	DEBUG_TRY					try{
#define	DEBUG_CATCH(x)				}catch(...){log("CATCH: *** " x " crash()! ***\n"); }

#define	MAX_PORT_ID					59999
#define	MAX_PACKET_ID				59999
#define	MAX_MSG_TYPE				59999

#define	PORT_ANY					-1
#define	INVALID_PORT				MAX_PORT_ID+1
#define	PACKET_ANY					-1

enum 
{	
	STATUS_FLAG_OK,				
	STATUS_FLAG_CLOSE,							
	STATUS_FLAG_ERROR,						
	STATUS_FLAG_FATAL,			
};


enum VAR_TYPE 
{	
	VARTYPE_NONE = MAX_MSG_TYPE+1,
	VARTYPE_CHAR,
	VARTYPE_UCHAR, 
	VARTYPE_SHORT,
	VARTYPE_USHORT, 
	VARTYPE_LONG, 
	VARTYPE_ULONG, 
	VARTYPE_INT,
	VARTYPE_UINT, 
	VARTYPE_FLOAT,
	VARTYPE_DOUBLE 
};

#define	STRUCT_TYPE(x)				((VAR_TYPE)sizeof(x))
#define	STRING_TYPE(x)				((VAR_TYPE)(strlen(x)+1))
#define	BUFFER_TYPE(x)				((VAR_TYPE)(x))


enum
{
	MSGPORT_SHELL			= 0,			
	MSGPORT_SOCKET			= 1,			
	MSGPORT_WORLD			= 2,				
};


struct CLIENTMSG_PACKET0
{
	int		idSocket;
	int		idPacket;
	int		size;
	char	buf[1];	
};

struct CProtocolMsgStruct
{
	unsigned short	msgSize;
	unsigned short	idPacket;
	char			msg[1];
};

typedef	char		MESSAGESTR[MAX_MESSAGESIZE];

struct SENDCLIENTMSG_PACKET0
{
	int		idSocket;			
	int		idPacket;
	int		size;
	char	buf[1];				
};
